<?php

namespace application\search\controller;

use ticky\controller;
use ticky\config;

class index extends controller {

    /**
     * 首页
     */
    public function index() {
        $q = str_replace('%', '', new_html_special_chars(strip_tags(trim($_GET['q']))));
        if (strlen($q) <= 2 || strlen($q) >= 30) {
            showmsg('你输入的字符过长或过短！', 'stop');
        }
        $modelid = isset($_GET['modelid']) ? intval($_GET['modelid']) : 1;
        $modelarr = get_modelinfo();
        if (!isset($modelarr[$modelid])) {
            showmsg('模型不存在！', 'stop');
        }
        $seo_title = '‘' . $q . '’的搜索结果_' . $site['site_name'];
        $keywords = $q . ',' . $site['site_keyword'];
        $description = $site['site_description'];
        $where = "`title` LIKE '%$q%' AND `status` = 1";
        $tablename = $modelarr[$modelid]['tablename'];
        $prefix = config::get('db')['prefix'];
        $ret = db('content a')
                ->field('title,description,inputtime,updatetime,views,thumb,url,catid')
                ->join($prefix . '' . $tablename . " g on a.contentid=g.contentid")
                ->where($where)
                ->newpage();

        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->display('search', 'home');
    }

}
